//  BackgroundLayer.m
//  SpaceViking
#import "BackgroundLayer.h"
#import "Constants.h"
#import "TreeCache.h"
#import "GameManager.h"

@implementation BackgroundLayer
@synthesize turn;
@synthesize angle;


static BackgroundLayer* instanceOfBackgroundLayer;

+ (BackgroundLayer*) sharedBackgroundLayer
{
	NSAssert(instanceOfBackgroundLayer != nil, @"GameScene instance not yet initialized!");
	return instanceOfBackgroundLayer;
}

- (id)init { 
    self = [super init];                                       
    if (self != nil) { 
        instanceOfBackgroundLayer = self;
        self.isAccelerometerEnabled = YES;
        CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
        turn = STRIGHT;
        turnCurrent = STRIGHT;
        
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
            // Indicates game is running on iPad
            backgroundImageScroll = [CCSprite spriteWithFile:@"bg0.png"];
            backgroundImageAnimated = [CCSprite spriteWithFile:@"bg1_stright1.png"];
            
        } else {
            // Not support Iphone
        }
        backgroundImageScroll.anchorPoint = ccp(0.5, 0);
        [backgroundImageScroll setPosition:CGPointMake(screenSize.width/2, screenSize.height * 0.6)];
        
        CCAnimation *animationBackgroundStright = [CCAnimation animation];
        [animationBackgroundStright addFrameWithFilename:@"bg1_stright1.png"];
        [animationBackgroundStright addFrameWithFilename:@"bg1_stright2.png"];
        [animationBackgroundStright addFrameWithFilename:@"bg1_stright3.png"];
        [animationBackgroundStright addFrameWithFilename:@"bg1_stright4.png"];
        
        CCAnimation *animationBackgroundLeft= [CCAnimation animation];
        [animationBackgroundLeft addFrameWithFilename:@"bg1_left1.png"];
        [animationBackgroundLeft addFrameWithFilename:@"bg1_left2.png"];
        [animationBackgroundLeft addFrameWithFilename:@"bg1_left3.png"];
        [animationBackgroundLeft addFrameWithFilename:@"bg1_left4.png"];
        
        CCAnimation *animationBackgroundRight = [CCAnimation animation];
        [animationBackgroundRight addFrameWithFilename:@"bg1_right1.png"];
        [animationBackgroundRight addFrameWithFilename:@"bg1_right2.png"];
        [animationBackgroundRight addFrameWithFilename:@"bg1_right3.png"];
        [animationBackgroundRight addFrameWithFilename:@"bg1_right4.png"];
        
        actionBackgroundStright = [[CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.4f 
                                                                                         animation:animationBackgroundStright
                                                                              restoreOriginalFrame:NO]]retain];
        actionBackgroundLeft = [[CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.4f 
                                                                                      animation:animationBackgroundLeft
                                                                           restoreOriginalFrame:NO]]retain];
        actionBackgroundRight = [[CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.4f 
                                                                                         animation:animationBackgroundRight
                                                                              restoreOriginalFrame:NO]]retain];
        [backgroundImageAnimated runAction:actionBackgroundStright];
        backgroundImageAnimated.anchorPoint = ccp(0.5, 0);
        [backgroundImageAnimated setPosition:CGPointMake(screenSize.width/2, -200)];
        
        [self addChild:backgroundImageScroll z:0 tag:1];
        [self addChild:backgroundImageAnimated z:1 tag:0];
        [self scheduleUpdate];
                      
    }
    return self;                                            
}

- (void)accelerometer: (UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration {            

	// this determines how sensitive the accelerometer reacts (higher = more sensitive)
	float sensitivity = 10;

    
    float accelerationFraction = acceleration.y * sensitivity;
    Turn currentTurn = accelerationFraction <= TURN_LEFT_THRESOLD ? LEFT
    : accelerationFraction  >= TURN_RIGHT_THRESOLD ? RIGHT : STRIGHT;
    if (currentTurn != turn) {
        turn = currentTurn;
    }
    angle = accelerationFraction;   
    //CCLOG(@"turn: %f ",acceleration.y);
    CCLOG(@"turn: %f ",accelerationFraction);
    
}

- (void)update: (ccTime)deltaTime {
    backgroundImageScroll = (CCSprite*)[self getChildByTag:1];
    backgroundImageAnimated = (CCSprite*)[self getChildByTag:0];
    [backgroundImageScroll setRotation:angle];
    [backgroundImageAnimated setRotation:angle];
    
    //xet turn for background
    if(turn != turnCurrent) {
        if(turn == STRIGHT) {
            [backgroundImageAnimated stopAllActions];
            [backgroundImageAnimated runAction:actionBackgroundStright];
        }
        if( turn == RIGHT) {
            [backgroundImageAnimated stopAllActions];
            [backgroundImageAnimated runAction:actionBackgroundLeft];
        }
        else if(turn == LEFT) {
            [backgroundImageAnimated stopAllActions];
            [backgroundImageAnimated runAction:actionBackgroundRight];
        }
        turnCurrent = turn;
    }
}

- (void)dealloc {
	instanceOfBackgroundLayer = nil;
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
